﻿
namespace n_EXPcommon
{
using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

using n_VarType;
using n_UnitList;
using n_FunctionList;
using n_VarList;
using n_StructList;
using n_ImagePanel;
using n_MyObjectList;
using n_MyObject;
using n_HardModule;
using n_GVar;
using n_GUIcoder;
using n_MyFileObject;
using c_FormMover;
using n_EXP;
using n_UserFunctionIns;
using n_MyIns;
using n_EventIns;

//表达式元素集合类
static class EXPMember
{
	public const string ErrorConst = "(无效)";
	
	//初始化
	public static void Init()
	{
		//...
	}
	
	//根据用户指令成员列表,用回车分隔
	public static string[] GetUserInsMember( n_GroupList.Group gr, string InsGroupMes, string NeedType, List<string> InnerNameList, List<string> TypeList, bool isSingle, bool AddErrorItem )
	{
		string[] eList = new string[100];
		int Length = 0;
		
		foreach( MyObject mo in G.CGPanel.myModuleList ) {
			if( mo is UserFunctionIns ) {
				
				if( gr != null && gr.GMes != null && mo.GroupMes != gr.GMes || gr == null && mo.GroupMes != null ) {
					continue;
				}
				UserFunctionIns m = (UserFunctionIns)mo;
				
				if( gr != null && InsGroupMes != gr.GMes && gr.MM != null ) {
					
					MemberMes mmm = gr.MM.GetMemberMessageFromName( m.GetFuncName() );
					if( mmm != null && mmm.Type != n_MemberType.MemberType.VisitType.Public ) {
						continue;
					}
				}
				TypeList.Add( EXP.ENote.t_USER );
				InnerNameList.Add( m.GetFuncName() );
				string UserType = m.GetUserType();
				string Error = "";
				if( "?" + UserType != NeedType ) {
					Error = ErrorConst;
				}
				eList[ Length ] = Error + m.GetFuncCall();
				++Length;
			}
		}
		
		if( Length == 0 ) {
			return null;
		}
		string[] result = new string[ Length ];
		for( int n = 0; n < result.Length; ++n ) {
			result[ n ] = eList[ n ];
		}
		return result;
	}
	
	//根据组件名获取组件对象
	public static MyFileObject GetModule( string ModuleName )
	{
		foreach( MyObject mo in G.CGPanel.myModuleList ) {
			if( !( mo is MyFileObject ) ) {
				continue;
			}
			MyFileObject m = (MyFileObject)mo;
			if( m.Name != ModuleName ) {
				continue;
			}
			return m;
		}
		return null;
	}
	
	//根据组件名获取组件成员列表,用回车分隔
	public static string[] GetModuleMember( string NeedType, string ModuleName, List<string> VarTypeList, List<string> InnerNameList, List<string> TypeList, bool isSingle, bool AddErrorItem, bool AddLine )
	{
		bool ExistFunc = false;
		
		foreach( MyObject mo in G.CGPanel.myModuleList ) {
			if( !( mo is MyFileObject ) ) {
				continue;
			}
			MyFileObject m = (MyFileObject)mo;
			if( m.Name != ModuleName ) {
				continue;
			}
			int Length = 0;
			string[] eList = new string[ m.ElementList.Length + 128 ];
			
			for( int n = 0; n < m.ElementList.Length; ++n ) {
				
				string Error = "";
				
				//扫描分割线
				if( AddLine && m.ElementList[ n ][ 1 ].StartsWith( InterfaceType.Split ) ) {
					
					//添加分隔线
					TypeList.Add( EXP.ENote.t_SPLIT );
					eList[ Length ] = "-";
					InnerNameList.Add( m.ElementList[ n ][ 2 ] );
					VarTypeList.Add( "未用到" );
					++Length;
				}
				//扫描存储类型成员
				if( m.ElementList[ n ][ 1 ]== InterfaceType.Vtype && !m.ElementList[ n ][ 2 ].StartsWith( "OS_" ) ) {
					
					if( "?" + InterfaceType.Vtype != NeedType ) {
						Error = ErrorConst;
						if( !AddErrorItem ) {
							continue;
						}
					}
					if( isSingle ) {
						eList[ Length ] = ModuleName + " " + Error + m.ElementList[ n ][ m.GetLC() ];
						TypeList.Add( EXP.ENote.t_MVAR );
					}
					else {
						eList[ Length ] = Error + m.ElementList[ n ][ m.GetLC() ];
						TypeList.Add( EXP.ENote.t_MVAR + " " + ModuleName );
					}
					VarTypeList.Add( "?" + InterfaceType.Vtype );
					InnerNameList.Add( m.ElementList[ n ][ 2 ] );
					++Length;
				}
				//扫描函数成员
				if( m.ElementList[ n ][ 1 ].StartsWith( InterfaceType.Function_ ) && !m.ElementList[ n ][ 2 ].StartsWith( "OS_" ) ) {
					
					string[] tempTypeList = m.ElementList[ n ][ 1 ].Split( '_' );
					if( tempTypeList.Length >= 3 && tempTypeList[2] == FuncExtType.Old ) {
						continue;
					}
					bool IntToFix = ("?" + tempTypeList[1] == EXP.ENote.v_int32 || "?" + tempTypeList[1] == EXP.ENote.v_fix) && NeedType == EXP.ENote.v_fix;
					if( ("?" + tempTypeList[1] != NeedType && NeedType != EXP.ENote.v_bool) && !IntToFix ||
					    "?" + tempTypeList[1] != EXP.ENote.v_fix && "?" + tempTypeList[1] != EXP.ENote.v_int32 && "?" + tempTypeList[1] != EXP.ENote.v_bool && NeedType == EXP.ENote.v_bool ) {
						Error = ErrorConst;
						if( !AddErrorItem ) {
							continue;
						}
					}
					
					if( !ExistFunc && Length != 0 && AddErrorItem ) {
						//添加分隔线
						TypeList.Add( EXP.ENote.t_SPLIT );
						eList[ Length ] = "-";
						InnerNameList.Add( "-" );
						VarTypeList.Add( "未用到" );
						++Length;
					}
					ExistFunc = true;
					
					string[] temp = m.ElementList[ n ][ m.GetLC() ].Split( '#' );
					
					string R = "";
					if( isSingle ) {
						R = ModuleName + " ";
						TypeList.Add( EXP.ENote.t_MFUNC );
					}
					else {
						TypeList.Add( EXP.ENote.t_MFUNC + " " + ModuleName );
					}
					
					R += Error;
					for( int ii = 0; ii < temp.Length - 1; ++ii ) {
						try {
						if( tempTypeList[ii + 2][0] == '*' ) {
							R += temp[ii] + "( " + tempTypeList[ii + 2] + " )";
						}
						else {
							R += temp[ii] + "( ?" + tempTypeList[ii + 2] + " )";
						}
						} catch {
							MessageBox.Show( "ERROR <GetModuleMember> " + eList[ Length ] + ":" + temp.Length + "," + ii + " " + tempTypeList.Length );
						}
					}
					R += temp[ temp.Length - 1 ];
					eList[ Length ] = R;
					InnerNameList.Add( m.ElementList[ n ][ 2 ] );
					VarTypeList.Add( "?" + tempTypeList[1] );
					++Length;
				}
				//扫描变量成员
				if( (m.ElementList[ n ][ 1 ].StartsWith( InterfaceType.Var_ ) || m.ElementList[ n ][ 1 ].StartsWith( InterfaceType.LinkVar_ ))
				   		&& !m.ElementList[ n ][ 2 ].StartsWith( "OS_" ) ) {
					
					string[] tempTypeList = m.ElementList[ n ][ 1 ].Split( '_' );
					
					if( tempTypeList.Length >= 3 && tempTypeList[2] == FuncExtType.Old ) {
						continue;
					}
					
					bool IntToFix = ("?" + tempTypeList[1] == EXP.ENote.v_int32 || "?" + tempTypeList[1] == EXP.ENote.v_fix) && NeedType == EXP.ENote.v_fix;
					if( ("?" + tempTypeList[1] != NeedType && "*" + tempTypeList[1] != NeedType) && NeedType != EXP.ENote.v_void && NeedType != EXP.ENote.v_bool && !IntToFix ||
					    
					   //这里进行递归运算判断, 需求bool类型时, 允许int, bool, 和 Cstring
					   "?" + tempTypeList[1] != EXP.ENote.v_Cstring && "?" + tempTypeList[1] != EXP.ENote.v_int32 && "?" + tempTypeList[1] != EXP.ENote.v_fix && "?" + tempTypeList[1] != EXP.ENote.v_bool && NeedType == EXP.ENote.v_bool ) {
						Error = ErrorConst;
						if( !AddErrorItem ) {
							continue;
						}
					}
					if( (NeedType == "*" + tempTypeList[1] || NeedType == EXP.ENote.v_void) && (tempTypeList.Length < 3 || tempTypeList[ 2 ] != "w") ) {
						Error = ErrorConst;
						if( !AddErrorItem ) {
							continue;
						}
					}
					if( isSingle ) {
						eList[ Length ] = ModuleName + " " + Error + m.ElementList[ n ][ m.GetLC() ];
						TypeList.Add( EXP.ENote.t_MVAR );
					}
					else {
						eList[ Length ] = Error + m.ElementList[ n ][ m.GetLC() ];
						TypeList.Add( EXP.ENote.t_MVAR + " " + ModuleName );
					}
					InnerNameList.Add( m.ElementList[ n ][ 2 ] );
					VarTypeList.Add( "?" + tempTypeList[1] );
					++Length;
				}
				//扫描常量成员
				if( m.ElementList[ n ][ 1 ].StartsWith( InterfaceType.Const_ ) && !m.ElementList[ n ][ 2 ].StartsWith( "OS_" ) ) {
					
					string[] tempTypeList = m.ElementList[ n ][ 1 ].Split( '_' );
					if( "?" + tempTypeList[1] != NeedType && NeedType != EXP.ENote.v_bool ||
					   ( "?" + tempTypeList[1] != EXP.ENote.v_int32 && "?" + tempTypeList[1] != EXP.ENote.v_fix && NeedType == EXP.ENote.v_fix ) ||
					   "?" + tempTypeList[1] != EXP.ENote.v_int32 && "?" + tempTypeList[1] != EXP.ENote.v_fix && "?" + tempTypeList[1] != EXP.ENote.v_bool && NeedType == EXP.ENote.v_bool  ) {
						Error = ErrorConst;
						if( !AddErrorItem ) {
							continue;
						}
					}
					if( isSingle ) {
						eList[ Length ] = ModuleName + " " + Error + m.ElementList[ n ][ m.GetLC() ];
						TypeList.Add( EXP.ENote.t_MVAR );
					}
					else {
						eList[ Length ] = Error + m.ElementList[ n ][ m.GetLC() ];
						TypeList.Add( EXP.ENote.t_MVAR + " " + ModuleName );
					}
					InnerNameList.Add( m.ElementList[ n ][ 2 ] );
					VarTypeList.Add( "?" + tempTypeList[1] );
					++Length;
				}
			}
			//添加分隔线
			if( AddErrorItem ) {
				TypeList.Add( EXP.ENote.t_SPLIT );
				eList[ Length ] = "-";
				InnerNameList.Add( "-" );
				VarTypeList.Add( "未用到" );
				++Length;
			}
			
			//添加事件指令
			foreach( MyObject m1 in G.CGPanel.myModuleList ) {
				if( m1 is EventIns ) {
					EventIns em = (EventIns)m1;
					
					if( em.owner != m ) {
						continue;
					}
					
					//添加事件手工调用
					TypeList.Add( EXP.ENote.t_USER );
					
					string UserType = "void";
					string Error = "";
					if( "?" + UserType != NeedType ) {
						Error = ErrorConst;
						
						if( !AddErrorItem ) {
							continue;
						}
					}
					
					eList[ Length ] = Error + em.EventEntry;
					InnerNameList.Add( "-0" );
					VarTypeList.Add( "未用到" );
					++Length;
					
					//添加事件开关
					TypeList.Add( EXP.ENote.t_SYS );
					eList[ Length ] = Error + "启用 " + em.EventEntry;
					InnerNameList.Add( "-1" );
					VarTypeList.Add( "未用到" );
					++Length;
					
					TypeList.Add( EXP.ENote.t_SYS );
					eList[ Length ] = Error + "禁用 " + em.EventEntry;
					InnerNameList.Add( "-2" );
					VarTypeList.Add( "未用到" );
					++Length;
					
					TypeList.Add( EXP.ENote.t_SYS );
					eList[ Length ] = Error + "结束 " + em.EventEntry;
					InnerNameList.Add( "-3" );
					VarTypeList.Add( "未用到" );
					++Length;
					
					TypeList.Add( EXP.ENote.t_SPLIT );
					eList[ Length ] = "-";
					InnerNameList.Add( "-" );
					VarTypeList.Add( "未用到" );
					++Length;
				}
			}
			//格式化输出结果
			string[] result = new string[ Length ];
			for( int n = 0; n < result.Length; ++n ) {
				result[ n ] = eList[ n ].Replace( '+', ' ' );
			}
			return result;
		}
		return null;
	}
	
	//获取全局变量列表
	public static string[] GetVarlist( bool isExt, n_GroupList.Group gr, string InsGroupMes, string NeedType, List<string> VarTypeList, List<string> InnerNameList, List<string> TypeList, MyObject o )
	{
		string s = null;
		
		//添加所在函数的形参名称
		if( gr == null ) {
			if( o != null ) {
				MyIns mi = (MyIns)o;
				while( mi.PreIns != null ) {
					mi = mi.PreIns;
				}
				if( mi is n_UserFunctionIns.UserFunctionIns ) {
					string[] c = ((n_UserFunctionIns.UserFunctionIns)mi).GetVarList( NeedType, TypeList, VarTypeList );
					for( int n = 0; c != null && n < c.Length; ++n ) {
						s += c[n] + ";";
						InnerNameList.Add( c[n] );
					}
				}
			}
			//添加分隔线
			if( s != null ) {
				TypeList.Add( EXP.ENote.t_SPLIT );
				s += "-;";
				InnerNameList.Add( "-" );
				VarTypeList.Add( "未用到" );
			}
		}
		
		foreach( MyObject m in G.CGPanel.myModuleList ) {
			
			
			
			//MessageBox.Show( m.Name );
			//if( gr != null && gr.Name != null ) {
			//	MessageBox.Show( "A: " + m.Name + ":" + gr.Name + ":" + m.GroupMes );
			//}
			
			
			
			if( gr != null && gr.GMes != null && m.GroupMes != gr.GMes || !isExt && gr == null && m.GroupMes != null ) {
				continue;
			}
			
			if( !( m is GVar ) ) {
				continue;
			}
			GVar gv = (GVar)m;
			
			
			if( gr != null && InsGroupMes != gr.GMes && gr.MM != null && gr.MM.GetMemberMessageFromName( gv.Name ).Type != n_MemberType.MemberType.VisitType.Public ) {
				continue;
			}
			
			if( "?" + gv.VarType == NeedType ||
			    "*" + gv.VarType == NeedType ||
			    (NeedType == EXP.ENote.v_fix && "?" + gv.VarType == EXP.ENote.v_int32) ||
			    (NeedType == EXP.ENote.v_int32 && "?" + gv.VarType == EXP.ENote.v_fix) ||
			    (NeedType == EXP.ENote.v_void && ("?" + gv.VarType == EXP.ENote.v_bool || "?" + gv.VarType == EXP.ENote.v_int32 || "?" + gv.VarType == EXP.ENote.v_fix) ) ||
			    (NeedType == EXP.ENote.v_void && ("*" + gv.VarType == EXP.ENote.v_bool || "*" + gv.VarType == EXP.ENote.v_int32 || "*" + gv.VarType == EXP.ENote.v_fix) ) ||
			    NeedType == EXP.ENote.v_bool && ("?" + gv.VarType == EXP.ENote.v_int32 || "?" + gv.VarType == EXP.ENote.v_fix)
			    //NeedType == EXP.ENote.v_bool && ("*" + gv.VarType == EXP.ENote.v_int32 || "*" + gv.VarType == EXP.ENote.v_fix) || //这里好像永远执行不到
				 ) {
				
				//这里拦截图片常量属性
				if( NeedType == EXP.ENote.v_m_bitmap && "?" + gv.VarType == EXP.ENote.v_bitmap && gv.Value != "" ) {
					continue;
				}
				
				TypeList.Add( EXP.ENote.t_VAR );
				VarTypeList.Add( "?" + gv.VarType );
				InnerNameList.Add( gv.Name );
				s += m.Name + ";";
			}
		}
		if( s == null ) {
			return null;
		}
		return s.TrimEnd( ';' ).Split( ';' );
	}
}
}

